/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.delivery.transfers;

import com.mycompany.delivery.dto.DeliveryDTO;
import com.mycompany.delivery.dto.UserDTO;
import com.mycompany.delivery.entities.Courier;
import com.mycompany.delivery.entities.Delivery;
import com.mycompany.delivery.entities.Employee;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author mhunek
 */
public class UserT {

    public static Courier dTOtoCourierEntity(UserDTO userDTO) {
        if (userDTO == null) {
            return null;
        }

        Courier courier = new Courier();
        courier.setContact(ContactT.dTOtoEntity(userDTO.getContact()));
        courier.setLogin(userDTO.getLogin());
        courier.setId(userDTO.getId());
        Set<DeliveryDTO> delll = userDTO.getDeliveries();
        Delivery del;
        Set<DeliveryDTO> deliveries = new HashSet<>(userDTO.getDeliveries());
        for (DeliveryDTO deliveryDTO : deliveries) {
            del = DeliveryT.dTOtoEntity(deliveryDTO);
            courier.addDelivery(DeliveryT.dTOtoEntity(deliveryDTO));
        }

        return courier;
    }

    public static Employee dTOtoEmployeeEntity(UserDTO userDTO) {
        if (userDTO == null) {
            return null;
        }

        Employee employee = new Employee();
        employee.setContact(ContactT.dTOtoEntity(userDTO.getContact()));
        employee.setLogin(userDTO.getLogin());
        employee.setId(userDTO.getId());
        for (DeliveryDTO deliveryDTO : userDTO.getDeliveries()) {
            employee.addDelivery(DeliveryT.dTOtoEntity(deliveryDTO));
        }

        return employee;
    }

    public static UserDTO EntityToDTO(Employee employee) {
        if (employee == null) {
            return null;
        }

        UserDTO userDTO = new UserDTO();
        userDTO.setContact(ContactT.entityToDTO(employee.getContact()));
        userDTO.setId(employee.getId());
        userDTO.setLogin(employee.getLogin());

        for (Delivery delivery : employee.getCreatedDelivery()) {
            userDTO.addDelivery(DeliveryT.EntityToDTO(delivery));
        }

        return userDTO;
    }

    public static UserDTO EntityToDTO(Courier courier) {
        if (courier == null) {
            return null;
        }

        UserDTO userDTO = new UserDTO();
        userDTO.setContact(ContactT.entityToDTO(courier.getContact()));
        userDTO.setId(courier.getId());
        userDTO.setLogin(courier.getLogin());

        for (Delivery delivery : courier.getDeliveries()) {
            userDTO.addDelivery(DeliveryT.EntityToDTO(delivery));
        }

        return userDTO;
    }

}
